const weakMap = new WeakMap();

const ob = new ResizeObserver((entries) => {
  for (const entrie of entries) {
    if (weakMap.has(entrie.target)) {
      const fn = weakMap.get(entrie.target);
      fn(entrie.contentRect);
    }
  }
});

export default {
  mounted(el, binding) {
    const fn = binding.value;
    ob.observe(el);
    weakMap.set(el, fn);
  },
  unmounted(el) {
    ob.unobserve(el);
    weakMap.delete(el);
  },
};
